﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace BanHangCshap.Class
{
    public static class ThongTinSuDung
    {
        
        public static int id = 0;
        
        public static string tendoanhnghiep = "";
        public static string diachi = "";
        public static string dienthoai = "";
        public static string nguoidaidien = "";
        public static string fax = "";
        public static string email = "";
        public static string website = "";
        public static string sotaikhoan = "";
        public static string masothue = "";
        public static int somautin = 0;
        public static bool registered = false;
        public static string clientkey = "";
        public static string productkey = "";
        public static Boolean isExistThongTinSuDung()
        {
            string sql = "select * from ThongTinSuDung";
            SqlDataAdapter da = new SqlDataAdapter();
            da = new SqlDataAdapter(sql, DatabaseStatic.conn);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
               
            }
            catch (Exception)
            {
                dt = null;
               
            }
            if (dt.Rows.Count > 0)
                return true;
            else return false;

        }
        public static void layThongTinSuDung()
        {
            string sql = "select * from ThongTinSuDung";
            SqlDataAdapter da = new SqlDataAdapter();
            da = new SqlDataAdapter(sql, DatabaseStatic.conn);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    tendoanhnghiep = dr["tendoanhnghiep"].ToString();
                    diachi = dr["diachi"].ToString();
                    nguoidaidien = dr["nguoidaidien"].ToString();
                    dienthoai = dr["dienthoai"].ToString();
                    nguoidaidien = dr["nguoidaidien"].ToString();
                    fax = dr["fax"].ToString();
                    email = dr["email"].ToString();
                    website = dr["website"].ToString();
                    sotaikhoan = dr["sotaikhoan"].ToString();
                    masothue = dr["masothue"].ToString();
                    clientkey = dr["clientkey"].ToString();
                    productkey = dr["productkey"].ToString();
                }
            }
            catch (Exception)
            {
                dt = null;
               
            }

            

        }
        public static Boolean suaThongTinSuDung(string tendoanhnghiep, string diachi, string nguoidaidien, string dienthoai,string fax, string email,  string sotaikhoan, string masothue)
        {
            string sql = "Update ThongTinSuDung set tendoanhnghiep = N'" + tendoanhnghiep + "', diachi = N'" + diachi + "', nguoidaidien = N'" + nguoidaidien + "', dienthoai = '" + dienthoai + "',";
            sql += "fax = '" + fax + "', email = '" + email + "',sotaikhoan = '" + sotaikhoan + "',masothue = '" + masothue + "'";
            SqlCommand cmd = new SqlCommand(sql, DatabaseStatic.conn);
            try
            {
                cmd.ExecuteNonQuery();
                
                
            }
            catch (Exception)
            {

                
                return false;
            }
            return true;
        }
        public static Boolean themThongTinSuDung(string tendoanhnghiep, string diachi, string nguoidaidien, string dienthoai, string fax, string email, string sotaikhoan, string masothue,string ck,string pk)
        {

            string sql = "Insert ThongTinSuDung(id, tendoanhnghiep,diachi,nguoidaidien,dienthoai,fax,email,sotaikhoan,masothue,clientkey, productkey) ";
            sql += "values ('" + UltitityStatic.MD5(tendoanhnghiep) + "',N'" + tendoanhnghiep + "', N'" + diachi + "',  N'" + nguoidaidien + "',  '" + dienthoai + "',";
            sql += " '" + fax + "',  '" + email + "', '" + sotaikhoan + "', '" + masothue + "','" + ck + "','" + pk + "')";
            SqlCommand cmd = new SqlCommand(sql, DatabaseStatic.conn);
            try
            {
                cmd.ExecuteNonQuery();


            }
            catch (Exception)
            {


                return false;
            }
            return true;
        }
        public static Boolean isRegistered()
        {
            //if (clientkey.Trim().Equals(UltitityStatic.HardSerial().Trim()) == false)
            //{
            //    //UltitityStatic.thongBaoLoi("Client key không chính xác.");
            //    return false;
            //}
            string key = UltitityStatic.MD5(clientkey.Trim()).ToUpper();
            if (key.Equals(productkey.Trim()))
                return true;
            return false;
        }
        public static Boolean dangKiSuDung(string productkey)
        {
            string sql = "Update ThongTinSuDung set productkey = '" + productkey + "'";
            
            SqlCommand cmd = new SqlCommand(sql, DatabaseStatic.conn);
            try
            {
                cmd.ExecuteNonQuery();


            }
            catch (Exception)
            {


                return false;
            }
            return true;
        }
        public static string headerreport()
        {
            return tendoanhnghiep + "\n" + diachi + "\nĐT: " + dienthoai;
        }


    }
}
